// $Id: filehandler.cpp 63 2004-10-22 14:33:20Z jonas $
#include "filehandler.h"
#include "distributions.h"

// Inititialize static variables
std::vector<File *> FileHandler::m_files;

/**
 * Free all allocated memory.
 */
FileHandler::~FileHandler()
{
	for (vector<File *>::iterator file=m_files.begin(); file!=m_files.end(); file++) {
		delete *file;
	}
	m_files.clear();
}

/**
 * Create lots of files of various sizes, all with unique ids.
 */
void FileHandler::init(int numberOfFiles)
{
	for (int i=0; i<numberOfFiles; i++) {
		m_files.push_back(new File(rand(), Distributions::getInstance()->fileSize(rand())));
	}
}

/**
 * Get a pointer to one of the files by its index.
 *
 * @param index An index (starting with 0) specifying which file to get. An
 *  index too large will return a null pointer.
 * @returns A pointer to a file (or 0 if the file index was too large)
 */
File *FileHandler::getFileByIndex(int index)
{
	if ((unsigned int) index >= m_files.size()) {
		return 0;
	}
	return m_files[index];
}
